<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>course</title>
  <link rel="stylesheet" href="../layui.css" media="all">
</head>
<body class = "courseMessageTab">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
</fieldset>
<form class="layui-form" action="/admin/course/save" method="post">
  <div>
  <div class="layui-form-item">
    <label class="layui-form-label">课程编码</label>
    <div class="layui-input-inline">
      <input type="text" name="courseSerial" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input courseSerial">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">课程名称</label>
    <div class="layui-input-inline">
      <input type="text" name="courseName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
    <div class="layui-form-item">
    <label class="layui-form-label">授课老师</label>
    <div class="layui-input-inline">
      <select name="tName" id="chooseTeacher" lay-verify="required" lay-search="">
        <option value=""></option>
      </select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">上课学期</label>
    <div class="layui-input-inline">
      <select name="classTrem" lay-filter="aihao">
        <option value=""></option>
        <option value="2018-2019学年第一学期" selected="">2018-2019学年第一学期</option>
        <option value="2018-2019学年第二学期" >2018-2019学年第二学期</option>
        <option value="2019-2020学年第一学期">2019-2020学年第一学期</option>
        <option value="2019-2020学年第二学期">2019-2020学年第二学期</option>
      </select>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
 </div>
</form>
<script src="../layui.js" charset="utf-8"></script>

<script src="../jquery-3.1.1.min.js"></script>

<script>
//获得所有的课程信息
$.ajax({
  url:'/admin/course/listCourse',
  type:'get',
  dataType:"json",
  data:{},
  success:function(result){
    var max = 0;
    for(var i=0;i<result.length;i++){
      if(result[i].courseSerial>max)
        max = result[i].courseSerial;
    }
    max = max +1;
    $(".courseSerial").val(max);
  },
  error:function(result){
    console.log(result);
  }
})


layui.use(['form', 'layedit', 'laydate'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate;
  //获得所有教师列表
  setMessage();

  function setMessage(){
    $.ajax({
      url:'/admin/user/listUser',
      type:'get',
      dataType:"json",
      data:{},
      success:function(result){
        var optionText;
        for(var i=0;i<result.length;i++){
          optionText = optionText + "<option value="+result[i].tName+">"+result[i].tName+"</option>";
        }
        $("#chooseTeacher").append(optionText);
        form.render('select');
      },
      error:function(result){
        console.log(result);
      }
    })
  }
  //监听提交
  form.on('submit(demo1)', function(data){
    saveCourse(data.field);
   /*  layer.alert(JSON.stringify(data.field), {
      title: '最终的提交信息'
    }) */
    return false;
  });
});
function saveCourse(a){
  $.ajax({
    url:'/admin/course/save',
    type:'post',
    dataType:'json',
    data:{
      "classTrem":a.classTrem,
      "courseName":a.courseName,
      "courseSerial":a.courseSerial,
      "tName":a.tName
    },
    success:function(result){
      alert("添加成功");
    },
    error:function(result){
      console.log(result);
    }
  })
}
</script>
</body>
</html>